package cn.bjxy.shishaolong.system.profession_course.service;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.system.profession_course.entity.AssinCourse;
import cn.bjxy.shishaolong.system.profession_course.entity.Course;
import cn.bjxy.shishaolong.system.profession_course.entity.Profession;
import cn.bjxy.shishaolong.system.profession_course.entity.ProfessionCourseRelation;
import cn.bjxy.shishaolong.system.profession_course.mapper.CourseMapper;
import cn.bjxy.shishaolong.system.profession_course.mapper.ProfessionCourseRelationMapper;
import cn.bjxy.shishaolong.system.profession_course.mapper.ProfessionMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-12
 */
@Service
public class ProfessionServiceImpl extends ServiceImpl<ProfessionMapper, Profession> implements IProfessionService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ProfessionCourseRelationMapper relationMapper;

    @Override
    public IPage<Profession> queryByCondition(IPage<Profession> page, Profession profession) {
        // 查询条件为空
        if (profession == null || StringUtils.isEmpty(profession.getProName())) {
            return this.page(page);
        }
        // 按条件查询
        return this.page(page, new QueryWrapper<Profession>().lambda()
                .like(Profession::getProName, profession.getProName()));
    }

    @Override
    public List<Profession> queryByCondition(Profession profession) {
        // 查询条件为空
        if (profession == null || StringUtils.isEmpty(profession.getProName())) {
            return this.list();
        }
        // 按条件查询
        return this.list(new QueryWrapper<Profession>().lambda()
                .like(Profession::getProName, profession.getProName()));
    }

    @Override
    public AssinCourse getAssinCourseData(@NotNull String professionId) {
        AssinCourse assinCourse = new AssinCourse();
        List<Map<String,Object>> courseItemList = new ArrayList<>();
        // 获取全部课程列表
        List<Course> courses = courseMapper.selectList(null);
        for (Course course : courses) {
            HashMap<String, Object> courseItem = new HashMap<>();
            courseItem.put(AssinCourse.COURSE_ITEM_KEY, course.getTid());
            courseItem.put(AssinCourse.COURSE_ITEM_LABEL, course.getCourseName());
            courseItemList.add(courseItem);
        }
        assinCourse.setAllCourseData(courseItemList);

        // 获取专业已选的课程列表
        List<Integer> selectList = new ArrayList<>();
        List<ProfessionCourseRelation> relationList = relationMapper.selectList(new QueryWrapper<ProfessionCourseRelation>().lambda()
                .eq(ProfessionCourseRelation::getProfessionId, professionId));
        for (ProfessionCourseRelation relation : relationList) {
            selectList.add(relation.getCourseId());
        }
        assinCourse.setSelectedCourse(selectList);

        return assinCourse;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAsignCourse(AssinCourse assinCourse) {

        // 1.删除当前专业所分配的课程
        relationMapper.delete(new QueryWrapper<ProfessionCourseRelation>().lambda()
            .eq(ProfessionCourseRelation::getProfessionId,assinCourse.getTid()));

        // 2.创建对应关系 并插入到专业课程对应关系表中
        List<Integer> selectedCourse = assinCourse.getSelectedCourse();
        for (Integer courseId: selectedCourse) {
            ProfessionCourseRelation relation = new ProfessionCourseRelation()
                    .setProfessionId(assinCourse.getTid()).setCourseId(courseId);
            relationMapper.insert(relation);
        }
    }

}
